﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace cpe.cpein.UI.Forms
{
    /// <summary>
    /// Interaction logic for FConnection.xaml
    /// </summary>
    public partial class FConnection : Window
    {
        #region Variables declaration

        UserCpeInServiceReference.UsersServiceClient userService = new UserCpeInServiceReference.UsersServiceClient();

        #endregion

        #region Create and Open

        /// <summary>
        /// Default constructor.
        /// </summary>
        public FConnection()
        {
            InitializeComponent();
        }

        #endregion

        #region Events methods

        /// <summary>
        /// Called when the connection button is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void btnConnection_Click(object sender, EventArgs args)
        {
            if (!String.IsNullOrEmpty(this.txbLogin.Text) && !String.IsNullOrEmpty(this.txbPassword.Password))
            {
                if (this.chkNew.IsChecked == false)
                {
                    // Launch the connection Method
                    Connect(this.txbLogin.Text, this.txbPassword.Password);
                }
                else
                {
                    Register(this.txbLogin.Text, this.txbPassword.Password);
                }
            }
            else
                MessageBox.Show("Les champs 'Nom d'utilisateur' et 'Mot de passe' doivent être renseignés !", "Champs non renseignés", MessageBoxButton.OK, MessageBoxImage.Warning);
        }

        /// <summary>
        /// Called when the cancel button is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void btnCancel_Click(object sender, EventArgs args)
        {
            CloseApplication();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Method allow to close the application.
        /// </summary>
        private void CloseApplication()
        {
            if (System.Windows.MessageBox.Show("Voulez-vous vraiment quitter l'application ?", "Fermeture de l'application", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                this.Close();
        }

        /// <summary>
        /// Method allow to connect the user.
        /// </summary>
        /// <param name="login"></param>
        /// <param name="pass"></param>
        private void Connect(string login, string pass)
        {
            Tools.Tools.BeginWaitStatus();
            UserCpeInServiceReference.User usr = userService.ConnectUser(login, pass);
            Tools.Tools.EndWaitStatus();
            if (usr != null && usr.Id > 0)
            {
                FMainWindow app = new FMainWindow(usr);
                app.Show();
                this.Hide();
            }
            else
                MessageBox.Show("Utilisateur ou mot de passe inconnu .", "Connexion impossible", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        /// <summary>
        /// Method allow to register the user.
        /// </summary>
        /// <param name="login"></param>
        /// <param name="pass"></param>
        private void Register(string login, string pass)
        {
            Tools.Tools.BeginWaitStatus();
            UserCpeInServiceReference.User usr = userService.RegisterUser(login, pass);
            Tools.Tools.EndWaitStatus();
            if (usr != null && usr.Id > 0)
            {
                FMainWindow app = new FMainWindow(usr);
                app.Show();
                this.Hide();
            }
            else
                MessageBox.Show("Le login est déjà utilisé par un autre utilisateur.", "Register impossible", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        #endregion

    }
}
